<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use DB;

class HuatiUserNexus extends Model
{
    protected $table = 'tbl_huati_user_nexus';
    public $timestamps = false;


    #######关联关系 start#########
    public function user()
    {
        return $this->belongsTo(User::class, 'user_id', 'c_id');
    }

    public function huati()
    {
        return $this->belongsTo(Huati::class, 'huati_id', 'id');
    }

    ######关联关系  end##########


    const STATUS_ON = 1;
    const STATUS_OFF = 2;

    static $StatusDisplay = [
        self::STATUS_ON => '正常',
        self::STATUS_OFF => '拉黑',
    ];

    const PAGE_SHOW = 20;

    /**
     * 圈子用户列表
     * @param $huati_id
     * @param $page
     * @param $name
     * @param $status
     * @return array
     */
    static function UserList($huati_id, $page, $name, $status, $user_id)
    {
        $datas = [];
        $result = HuatiUserNexus::with('user:c_id,name,sex,c_avatar,phone')->where('huati_id', $huati_id)->where('user_id', '!=', $user_id);
        //名字
        if ($name) {
            $user_ids = User::where('name', 'like', '%' . $name . '%')->pluck('c_id')->toArray();
            $result->whereIn('user_id', $user_ids);
        }
        //状态
        if ($status) {
            $result->where('status', $status);
        }

        $number = $result->count();
        $pages = ceil($number / self::PAGE_SHOW);
        if (!$page) {
            $page = 1;
        }
        $lists = $result->skip(($page - 1) * self::PAGE_SHOW)->take(self::PAGE_SHOW)->get();
        if (count($lists) > 0) {
            foreach ($lists as $list) {
                $datas[] = [
                    'id' => $list->id,
                    'user_name' => $list->user->name,
                    'user_sex' => $list->user->sex,
                    'user_avatar' => $list->user->c_avatar,
                    'user_status' => $list->status,
                    'user_ban_reason' => $list->status === self::STATUS_OFF ? $list->ban_reason : '',
                    'user_ban_time' => $list->status === self::STATUS_OFF ? date('Y-m-d H:i:s', $list->ban_time) : '',
                    'user_phone' => hidePhoneNum($list->user->phone),
                    'time' => $list->ctime,
                ];
            }
        }

        return success(compact('datas', 'number', 'pages'));
    }

    /**
     * 禁言
     * @param $request
     * @return array
     * @throws \Exception
     */
    static function Ban($request)
    {
        DB::beginTransaction();
        $huati_user_nexus = HuatiUserNexus::find($request->id);
        //修改状态
        if ($huati_user_nexus->status === HuatiUserNexus::STATUS_OFF) {
            $huati_user_nexus->status = HuatiUserNexus::STATUS_ON;
            if (!$huati_user_nexus->save()) {
                DB::rollBack();

                return error('失败');
            }
        } else {
            if (!$request->has('ban_time') || !$request->has('ban_reason') || !is_numeric($request->ban_time)) {
                return error('参数不对');
            }
            $huati_user_nexus->status = HuatiUserNexus::STATUS_OFF;
            $huati_user_nexus->ban_time = time() + ($request->ban_time * 24 * 3600);
            $huati_user_nexus->ban_reason = $request->ban_reason;
            if (!$huati_user_nexus->save()) {
                DB::rollBack();

                return error('失败');
            }
            //添加系统消息
            if (!DB::table('jpush')->insert(
                [
                    'title' => '系统消息',
                    'content' => $request->ban_reason,
                    'ctime' => date('Y-m-d H:i:s', time()),
                    'user_id' => $huati_user_nexus->user_id
                ]
            )) {
                DB::rollBack();

                return error('失败');
            }
            //发送系统消息
            $params['pcs'] = 3;
            $params['alias'] = $huati_user_nexus->user_id;
            $params['content'] = $request->ban_reason;
            $params = http_build_query($params);
            $url = 'https://wechatlt.jkbanlv.com/push/SendToOne?' . $params;
            $this_header = array(
                "content-type: application/x-www-form-urlencoded; charset=UTF-8"
            );

            $curl = curl_init(); // 启动一个CURL会话
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_HTTPHEADER, $this_header);
            curl_setopt($curl, CURLOPT_HEADER, 0);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);  // 从证书中检查SSL加密算法是否存在
            $tmpInfo = curl_exec($curl);     //返回api的json对象
            $tmpInfo = json_decode($tmpInfo, true);
            //关闭URL请求
            curl_close($curl);
            if (!isset($tmpInfo['code']) || $tmpInfo['code'] !== 200 || $tmpInfo['msg']['responseCode'] !== 200) {
                DB::rollBack();

                return error('失败');
            }
        }
        DB::commit();

        return success(['message' => '成功']);
    }
}
